package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.Dept;
import com.ruoyi.system.service.DaochuService;
import com.ruoyi.system.service.IRenwuService;
import com.ruoyi.system.service.IZhuanjiaService;
import com.ruoyi.system.service.IZhuanjiabiandongshenbaoService;
import com.ruoyi.web.controller.tool.JsonResult;
import com.ruoyi.web.controller.tool.PageInfo;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/system/daochu")
public class DaochuController {

    private String prefix = "system/daochu";

    @Autowired
    DaochuService daochuService;

    @Autowired
    private IZhuanjiaService zhuanjiaService;

    @Autowired
    private IRenwuService renwuService;

    @Autowired
    private IZhuanjiabiandongshenbaoService zhuanjiabiandongshenbaoService;


    @RequestMapping("deptxinxi")
    @ResponseBody
    public void deptxinxi(Dept dept, HttpServletResponse response) {
        daochuService.deptxinxi(dept,response);
    }

    @RequestMapping("zhuanjiaxinxi")
    @ResponseBody
    public void zhuanjiaxinxi(String deptid,
                              String xingming,
                              String deptname,
                              String deptlevel,
                              String gongzuodanwei,
                              String ruxuannianfen,
                              String zhuanjiazhuangtai,
                              String zhuanjiazhuangtaibiandongshijian_kaishi,
                              String zhuanjiazhuangtaibiandongshijian_jieshu,
                              HttpServletResponse response) {
        //获取当前用户
        Subject subject = ShiroUtils.getSubject();
        List<Map<String, Object>> list = new ArrayList<>();
        //专家中心超管、专家中心普通管理员、单位超管查本单位和所有下属单位
        if (subject.hasRole("zhongxin_commonadmin")||subject.hasRole("zhongxin_superadmin")||subject.hasRole("dept_superadmin"))
        { list = zhuanjiaService.chaxuntongjilist(deptid,xingming,deptname,deptlevel,gongzuodanwei,ruxuannianfen, zhuanjiazhuangtai,zhuanjiazhuangtaibiandongshijian_kaishi, zhuanjiazhuangtaibiandongshijian_jieshu, 1, 99999);}
        //单位普通管理员查本单位
        if (subject.hasRole("dept_commonadmin"))
        {list = zhuanjiaService.chaxuntongjilistCommon(deptid,xingming,deptname,deptlevel,gongzuodanwei,ruxuannianfen, zhuanjiazhuangtai, zhuanjiazhuangtaibiandongshijian_kaishi, zhuanjiazhuangtaibiandongshijian_jieshu, 1, 99999);}
        daochuService.zhuanjiaxinxi(list,response);
    }

    @RequestMapping("bdshenbaoxinxi")
    @ResponseBody
    public void bdshenbaoxinxi(@RequestParam("deptid")String deptid,
                              @RequestParam("zhuanjiamingcheng")String zhuanjiamingcheng,
                              @RequestParam("biandongleixing")String biandongleixing,
                              @RequestParam("biandongriqikaishi")String biandongriqikaishi,
                              @RequestParam("biandongriqijieshu")String biandongriqijieshu,
                              ServletResponse servletResponse,
                              HttpServletResponse response,
                              HttpServletRequest request) {
        try {
            HttpServletResponse mmResponse = (HttpServletResponse) servletResponse;
            HttpSession session = request.getSession();
            String currentdeptid = session.getAttribute("deptid").toString();
            if(!deptid.equals(currentdeptid)){
                error("查询申报列表单位有误！", mmResponse);
                return;
            }
            List<Map<String,Object>> list = zhuanjiabiandongshenbaoService.selectZhuanjiaBiandongListCustom(deptid,zhuanjiamingcheng,biandongleixing,biandongriqikaishi,biandongriqijieshu,1,9999);
            daochuService.bdshenbaoxinxi(list,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("bdshenhexinxi")
    @ResponseBody
    public void bdshenhexinxi(@RequestParam("shenpideptid")String shenpideptid,
                                          @RequestParam("zhuangtai")String zhuangtai,
                                          @RequestParam("biandongleixing")String biandongleixing,
                                          @RequestParam("deptname")String deptname,
                                          @RequestParam("xingming")String xingming,
                                          @RequestParam("xiafashijiankaishi")String xiafashijiankaishi,
                                          @RequestParam("xiafashijianjieshu")String xiafashijianjieshu,
                                          @RequestParam("wanchengshijiankaishi")String wanchengshijiankaishi,
                                          @RequestParam("wanchengshijianjieshu")String wanchengshijianjieshu,
                                          ServletResponse servletResponse,
                                          HttpServletResponse response,
                                          HttpServletRequest request) {
        try {
            HttpServletResponse mmResponse = (HttpServletResponse) servletResponse;
            HttpSession session = request.getSession();
            String deptid = session.getAttribute("deptid").toString();
            if(!deptid.equals(shenpideptid)){
                error("查询审核列表单位有误！", mmResponse);
                return;
            }
            // 判断状态  0 未审核  1 通过  2驳回  9全部
            int shifouwancheng = 99;
            int shifoutongguo = 99;
            if(zhuangtai.equals("0")){
                shifouwancheng =0;
            }else if(zhuangtai.equals("1")){
                shifoutongguo = 1;
            }else if(zhuangtai.equals("2")){
                shifouwancheng =1;
                shifoutongguo = 2;
            }else if(zhuangtai.equals("9")){
                // 默认就是全部
            }else{
                error("查询审核列表类型有误！", mmResponse);
                return;
            }
            List<Map<String,Object>> list = renwuService.findBdShenheListByDept(shenpideptid,shifoutongguo,shifouwancheng,biandongleixing,deptname,xingming,
                    xiafashijiankaishi,xiafashijianjieshu,wanchengshijiankaishi,wanchengshijianjieshu,1,9999);
            daochuService.bdshenhexinxi(list,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void error(String string, HttpServletResponse mmResponse) {
        try {
            mmResponse.setCharacterEncoding("gbk");
            PrintWriter mmOut;
            mmOut = mmResponse.getWriter();
            mmOut.print("<div style='text-align:center;font-size:18px; font-weight:weight;'>" + string + "</div>");
            mmOut.flush();
            mmOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
